<div class="container-fluid" style="margin: 0px 15px 0px 15px;">
<ol class="breadcrumb">
  <li><a href="<?php echo base_url('/index.php/inicio');?>">Início</a></li>
  <li><a href="<?php echo base_url('index.php/eventos/consulta');?>">Audiência</a></li>
  <li class="active">Novo</li>
</ol>
			<fieldset>
			<legend>Cadastro Audiência/Perícia</legend>
				<?php
					$attributes = array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form');
					$hidden = array('idlocal' => '','idprocesso' => '');
					echo form_open('eventos/salvar',$attributes,$hidden); 
				
				?>
			<form class ="form-horizontal" role="form" id="form" method="POST">
				<div class="form-group">
				<label for="" class="col-sm-2 control-label">Processo</label>
				<div class="col-sm-2">
				  
				  <?php 
					$data = array('name'=> 'numero_processo','id'=> 'numero_processo','value'=> '','placeholder'=> 'Numero do Processo','class'=> 'form-control input-sm');
					echo form_input($data);
				?>
				</div>
				</div>
				<div class="form-group">
				<label for="" class="col-sm-2 control-label">Tipo</label>
				<div class="col-sm-2">
					<?php 
						$class = 'class="form-control input-sm"';
						echo form_dropdown('tipoevento', $tipoevento, '',$class);
					?>
				</div>
				</div>
				<!--<div class="form-group">
				<label for="" class="col-sm-2 control-label">Local</label>
				<div class="col-sm-3">
				<?php 
					/*$data = array('name'=> 'local','id'=> 'local','value'=> '','placeholder'=> 'Local','class'=> 'form-control input-sm');
					echo form_input($data);*/
					$class = 'class="form-control input-sm"';
					echo form_dropdown('local', $local, '',$class);
				?>
				</div>
				</div> -->
				<div class="form-group">
				<label for="" class="col-sm-2 control-label">Local</label>
				<div class="col-sm-3">
					<?php 
						$data = array('name'=> 'local','id'=> 'local','value'=> '','placeholder'=> 'Local','class'=> 'form-control input-sm');
						echo form_input($data);
					?>
				</div>
				<div class="col-sm-2 control-label" style="text-align:left;color:red;">
					<?php echo form_error('autor');?>	
				</div>	
				</div>
				<div class="form-group">
				<label for="" class="col-sm-2 control-label"></label>
					<div class="col-sm-2">
					<a data-toggle="modal" href="#" class="btn btn-primary btn-xs" data-target="#mylocal"><span class="glyphicon glyphicon-plus"></span>Cadastrar local</a>
					</div>
				</div>
				
				<div class="form-group">
				<label for="" class="col-sm-2 control-label">Data</label>
				<div class="col-sm-2">
				<?php 
					$data = array('name'=> 'dia','id'=> 'dia','value'=> '','placeholder'=> 'Data','class'=> 'form-control input-sm');
					echo form_input($data);
				?>
				</div>
				</div>
				<!--<div class="form-group">
				<label for="" class="col-sm-2 control-label">Hora</label>
				<div class="col-sm-2">
				<?php 
					//$data = array('name'=> 'hora','id'=> 'hora','value'=> '','placeholder'=> 'Hora','class'=> 'form-control input-sm');
					//echo form_input($data);
				?>
				</div>
				</div> -->
				<div class="form-group">
				<label for="inputPassword3" class="col-sm-2 control-label">Observação</label>
				<div class="col-sm-4">
				<?php 
					$data = array('name'=> 'observacao','id'=> 'observacao','placeholder'=> 'Observação','class'=> 'form-control input-sm','value'=>@$processo->observacao,'rows'=>'3');
					echo form_textarea($data);
				?>
				</div>
				</div>
				<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<?php 
						$data = array('name'=>'salvar','type' => 'submit','class' => 'btn btn-primary','content' =>'Salvar');
						echo form_button($data);
					?>
					<a id="cancelar" href="<?php echo base_url('index.php/eventos/consulta');?>" class="btn btn-default"></span>Cancelar</a>
				</div>
				</div>

				</form>
			<div id="retorno"></div>
			</fieldset>

		</div>
		
		<div class="modal bs-example-modal-lg" id="mylocal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog modal-lg">
			<div class="modal-content">		
				<form class='form-horizontal' role='form' id='form_local' method="POST">
					<div class="modal-header btn-primary" >
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						 <h4 class="modal-title">Cadastro Local</h4>

					</div>
					<div class="modal-body">
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Nome Local</label>
							<div class="col-sm-5">
								<?php 
									$data = array('name'=> 'nomelocal','id'=> 'nomelocal','value'=> '','placeholder'=> 'Nome Local','class'=> 'form-control input-sm');
									echo form_input($data);
								?>
							</div>
							<div class="col-sm-2 control-label" style="text-align:left;color:red;">
							<?php echo form_error('assunto');?>	
							</div>	
						</div>
						<div id="retorno3"></div>					
					</div>
						<div class="modal-footer">
						<button type="submit" class="btn btn-primary" id="modallocal" >Salvar</button>
						<a href="#" type="button"  data-dismiss="modal" class="btn btn-default">Cancelar</a>
						</div>
				</form>
			</div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
		
		<script src="<?php echo base_url('includes/js/jquery.tokeninput.js')?>"></script>
		<link href="<?php echo base_url('includes/css/token-input-facebook.css') ?>" rel="stylesheet"> 
		
		<script language="javascript" type="text/javascript">
			$(document).ready(function(){//Equivalente ao window.onload porem mais rapido
				$("#numero_processo").focus();
				$('#dia').datetimepicker({dateFormat: "dd/mm/yy"});
				$("#form").validate({
					rules: {
						numero_processo: { 
							required: true, 
							remote:'<?php echo base_url('index.php/processos/validaProcessoAudiencia');?>'
						},
						dia: "required"
					},
					messages: {
						numero_processo: { required: 'Campo requerido', remote: ' Processo não existe!' },
						dia: "Campo requerido"
					},
					submitHandler: function(form){
						var dados = $('#form').serialize();
						$.ajax({
							type: "POST",
							url: "<?php echo base_url('index.php/eventos/salvar'); ?>",
							data: dados,
							success: function( data )
							{
								//alertify.success(data);
								$("html, body").animate({ scrollTop: $(document).height() }, "slow");
								if(data === 'OK'){
										$('#retorno').html('<div class="alert alert-success" role="alert">CADASTRO EFETUADO COM SUCESSO</div>');
										setTimeout(function() {
											$('#retorno').html('');
											window.location = "<?php echo base_url('index.php/eventos/novo'); ?>";
										}, 2000);
									
									}else{
										$('#retorno').html(data);
									}
							},
							statusCode: {
								404: function() {
									alertify.error("Pagina nao encontrada");
								}
							}
						});
						return false;
					}
				});				
				$( "#numero_processo" ).autocomplete({
					source: "<?php echo base_url('index.php/processos/consultaProcessos');?>",
					minLength: 2,
					select: function( event, ui ) {
						$("input[name='idprocesso']").val(ui.item.id);
					},
					
					
				});
				$("#local").tokenInput("<?php echo base_url('index.php/locais/consultaLocaisJson');?>", {
		        	theme: "facebook",
					searchDelay:"50",
					tokenLimit: "1",
					preventDuplicates: "true"
					
				});
				$("#modallocal").click(function(){
				$("#form_local").validate({
					rules: {
						nomelocal: "required"
					},
					messages: {
						nomelocal: "Campo requerido"
					},
					submitHandler: function(form){
						var dados = $('#form_local').serialize();
						$.ajax({
								type: "POST",
								url: "<?php echo base_url('index.php/locais/salvar');?>",
								data: dados,
								success: function( data )
								{
									if(data === 'OK'){
										$('#retorno3').html('<div class="alert alert-success" role="alert">CADASTRO EFETUADO COM SUCESSO</div>');
										setTimeout(function() {
											$('#retorno3').html('');
											$('#mylocal').modal('hide');
										}, 2000);
										$('#nomelocal').val('');
										
										//
									}else{
										$('#retorno3').html(data);
									}
								}
						});
						return false;
					}
				});
			});
			});
	</script>
	</body>
</html>